home *** CD-ROM | disk | FTP | other *** search
/ Champak 132 (Alt) / Vol 132.iso / games / rong.swf / scripts / %3Cdefault package%3E / CRoundPongGame.as < prev    next >
Encoding:
Text File  |  2011-06-09  |  7.9 KB  |  238 lines

  1. function CRoundPongGame()
  2. {
  3.    Mouse.addListener(this);
  4.    this._baseDepthExplosions = 1000;
  5.    this._scoreToWin = _root._gameOptions._scoreToWin;
  6.    _root._gameOptions._playerOptions[1]._RGBColor = !this._isInDemoMode ? 255 : 5592416;
  7.    _root._gameOptions._playerOptions[2]._RGBColor = !this._isInDemoMode ? 16711680 : 6313301;
  8.    _root._gameOptions._playerOptions[3]._RGBColor = !this._isInDemoMode ? 65280 : 5595221;
  9.    _root._gameOptions._playerOptions[4]._RGBColor = !this._isInDemoMode ? 16776960 : 6316117;
  10.    this.quit_button._visible = !this._isInDemoMode;
  11.    this._boundsColor = new Color(this["out of bounds line"]);
  12.    this.InitializeGame(_root._gameOptions);
  13.    this._ballHitSound = new Sound(this.puck1);
  14.    this._ballHitHumpSound = new Sound(this.puck1);
  15.    if(!this._isInDemoMode)
  16.    {
  17.       this._ballHitSound.attachSound("radarping.wav");
  18.       this._ballHitHumpSound.attachSound("pipebang");
  19.    }
  20. }
  21. function Wrap(x, min, max)
  22. {
  23.    var translated = (x - min) % (max - min);
  24.    return translated >= 0 ? min + translated : max + translated;
  25. }
  26. function WrapAngle(x)
  27. {
  28.    return Wrap(x,-180,180);
  29. }
  30. function Magnitude(vec)
  31. {
  32.    return Math.sqrt(vec.x * vec.x + vec.y * vec.y);
  33. }
  34. function MirrorVector(vec, angleRadians)
  35. {
  36.    var vecLength = Magnitude(vec);
  37.    var vecAngle = Math.atan2(vec.y,vec.x);
  38.    var deltaAngle = vecAngle - angleRadians;
  39.    var resultAngle = angleRadians - deltaAngle;
  40.    return {x:Math.cos(resultAngle) * vecLength,y:Math.sin(resultAngle) * vecLength};
  41. }
  42. function AngleDelta(angle1, angle2)
  43. {
  44.    return WrapAngle(angle1 - angle2);
  45. }
  46. CRoundPongGame.prototype = new MovieClip();
  47. Object.registerClass("CRoundPongGame",CRoundPongGame);
  48. CRoundPongGame.prototype.onMouseMove = function()
  49. {
  50.    if(!this.paddle1._isAIControlled)
  51.    {
  52.       this.paddle1._rotationTarget = Math.atan2(this._ymouse,this._xmouse) / 3.141592653589793 * 180;
  53.    }
  54. };
  55. CRoundPongGame.prototype.onEnterFrame = function()
  56. {
  57.    this._nFrames = this._nFrames + 1;
  58.    if(!this.paddle1._isAIControlled)
  59.    {
  60.       this.paddle1._rotation += !Key.isDown(39) ? 0 : - this._playerPaddleSpeed;
  61.       this.paddle1._rotation += !Key.isDown(37) ? 0 : this._playerPaddleSpeed;
  62.    }
  63.    if(this._cFramesSinceLastExplosion % 40 < 1)
  64.    {
  65.       this.CreateHumpExplosion();
  66.    }
  67.    this._cFramesSinceLastExplosion = this._cFramesSinceLastExplosion + 1;
  68. };
  69. CRoundPongGame.prototype.InitializeGame = function(gameOptions)
  70. {
  71.    this._cFramesSinceLastExplosion = 0;
  72.    this._oldXMouse = this._xmouse;
  73.    this.attachMovie("CPuck","puck1",10);
  74.    this._paddleHitAcceleration = _root._gameOptions._paddleHitAcceleration;
  75.    this._initialPuckSpeed = _root._gameOptions._initialPuckSpeed;
  76.    this._nPaddles = gameOptions._nPlayers;
  77.    var i;
  78.    i = 1;
  79.    while(i <= this._nPaddles)
  80.    {
  81.       this.attachMovie("CPaddle","paddle" + i,i,{_rotation:- i * (270 / (this._nPaddles - 1)),_arcLength:20,_isAIControlled:gameOptions._playerOptions[i]._isAIControlled,_maxRotationSpeed:(!gameOptions._playerOptions[i]._isExpert ? (!gameOptions._playerOptions[i]._isAIControlled ? 20 : 12) : (!gameOptions._playerOptions[i]._isAIControlled ? 10 : 7))});
  82.       var paddle = this["paddle" + i];
  83.       paddle._color = new Color(paddle);
  84.       paddle._color.setRGB(gameOptions._playerOptions[i]._RGBColor);
  85.       var scoreRotation = -135 + 90 * (i - 1);
  86.       if(this._nPaddles > 3)
  87.       {
  88.          var baseRotation = i >= 3 ? 45 : -135;
  89.          var offsetRotation = !(i & 1) ? -20 : 20;
  90.          scoreRotation = baseRotation + offsetRotation;
  91.       }
  92.       this.attachMovie("CScoreBoard","CScoreBoard" + i,10000 + i,{_rotation:scoreRotation});
  93.       this["CScoreBoard" + i].playerScore.textColor = gameOptions._playerOptions[i]._RGBColor;
  94.       i++;
  95.    }
  96.    this._ndxServingPlayer = 1;
  97.    this._ndxReceivingPlayer = this.WrapPlayerIndex(this._ndxServingPlayer + 1);
  98.    this.GetReceivingPlayer().BeginReceiving();
  99.    if(Math.random() < 0.5)
  100.    {
  101.       this.SwapServerAndReceiver();
  102.    }
  103.    this._boundsColor.setRGB(this.GetReceivingPlayer()._color.getRGB());
  104.    this.EnableRandomBackground();
  105.    clearInterval(_root._intervalID);
  106.    _root._intervalID = setInterval(this,"Serve",800);
  107. };
  108. CRoundPongGame.prototype.ShutdownGame = function()
  109. {
  110.    clearInterval(_root._intervalID);
  111.    this.removeMovieClip("puck1");
  112.    var i;
  113.    i = 1;
  114.    while(i <= this._nPaddles)
  115.    {
  116.       this.removeMoveClip("paddle" + i);
  117.       i++;
  118.    }
  119. };
  120. CRoundPongGame.prototype.BallHit = function()
  121. {
  122.    this._ballHitSound.start();
  123.    this.SwapServerAndReceiver();
  124.    if(this._nPaddles == 1)
  125.    {
  126.       this.paddle1.Score();
  127.       this.CScoreBoard1.playerScore.text = this.paddle1._score;
  128.    }
  129.    this._lengthVolley = this._lengthVolley + 1;
  130.    if(this._lengthVolley % 5 < 1)
  131.    {
  132.       this.puck1._velX *= this._paddleHitAcceleration;
  133.       this.puck1._velY *= this._paddleHitAcceleration;
  134.    }
  135. };
  136. CRoundPongGame.prototype.BallHitHump = function()
  137. {
  138.    this._ballHitHumpSound.start();
  139. };
  140. CRoundPongGame.prototype.SwapServerAndReceiver = function()
  141. {
  142.    this.GetReceivingPlayer().FinishedReceiving();
  143.    this._ndxServingPlayer = this.WrapPlayerIndex(++this._ndxServingPlayer);
  144.    this._ndxReceivingPlayer = this.WrapPlayerIndex(this._ndxServingPlayer + 1);
  145.    this.GetReceivingPlayer().BeginReceiving();
  146.    this._boundsColor.setRGB(this.GetReceivingPlayer()._color.getRGB());
  147.    this.CreateHumpExplosion();
  148. };
  149. CRoundPongGame.prototype.PuckOutOfBounds = function()
  150. {
  151.    if(this._nPaddles > 1)
  152.    {
  153.       this.GetServingPlayer().Score();
  154.       if(this._nPaddles > 2)
  155.       {
  156.          this.GetReceivingPlayer().ScoreFailure();
  157.       }
  158.    }
  159.    else
  160.    {
  161.       this.paddle1._score = 0;
  162.    }
  163.    this.puck1.GoIdle(true);
  164.    if(this._scoreToWin > 0 && this.GetServingPlayer()._score >= this._scoreToWin)
  165.    {
  166.       var i;
  167.       i = 1;
  168.       while(i <= this._nPaddles)
  169.       {
  170.          this["CScoreBoard" + i].playerScore.text = this["paddle" + i]._score;
  171.          i++;
  172.       }
  173.       _root.gotoAndPlay("victory announcement");
  174.    }
  175.    else
  176.    {
  177.       clearInterval(_root._intervalID);
  178.       _root._intervalID = setInterval(this,"Serve",800);
  179.    }
  180. };
  181. CRoundPongGame.prototype.GetServingPlayer = function()
  182. {
  183.    return this["paddle" + this._ndxServingPlayer];
  184. };
  185. CRoundPongGame.prototype.GetReceivingPlayer = function()
  186. {
  187.    return this["paddle" + this._ndxReceivingPlayer];
  188. };
  189. CRoundPongGame.prototype.Serve = function()
  190. {
  191.    clearInterval(_root._intervalID);
  192.    var i;
  193.    i = 1;
  194.    while(i <= this._nPaddles)
  195.    {
  196.       this["CScoreBoard" + i].playerScore.text = this["paddle" + i]._score;
  197.       i++;
  198.    }
  199.    this.puck1.GoIdle(false);
  200.    var launchAngle = Math.random() * 3.141592653589793 * 2;
  201.    this.puck1._velX = Math.cos(launchAngle) * this._initialPuckSpeed;
  202.    this.puck1._velY = Math.sin(launchAngle) * this._initialPuckSpeed;
  203.    this.puck1._x = this.puck1._velX / this._initialPuckSpeed * -60;
  204.    this.puck1._y = this.puck1._velY / this._initialPuckSpeed * -60;
  205.    this._lengthVolley = 1;
  206. };
  207. CRoundPongGame.prototype.EnableRandomBackground = function()
  208. {
  209.    var choice = Math.random();
  210.    this.background1._visible = false;
  211.    this.background2._visible = false;
  212.    this.background3._visible = false;
  213.    if(choice < 0.33)
  214.    {
  215.       this.background1._visible = true;
  216.    }
  217.    else if(choice < 0.66)
  218.    {
  219.       this.background2._visible = true;
  220.    }
  221.    else
  222.    {
  223.       this.background3._visible = true;
  224.    }
  225. };
  226. CRoundPongGame.prototype.WrapPlayerIndex = function(x)
  227. {
  228.    return Wrap(x,1,this._nPaddles + 1);
  229. };
  230. CRoundPongGame.prototype.CreateHumpExplosion = function()
  231. {
  232.    if(this._nActiveHumpExplosions < 2)
  233.    {
  234.       this._cFramesSinceLastExplosion = 0;
  235.       this.attachMovie("CHumpExplosion","CHumpExplosion" + ++this._nHumpExplosions,this._baseDepthExplosions + this._nHumpExplosions,{_x:-100,_y:100,_hue:this.GetReceivingPlayer()._color.getRGB(),_alpha:40});
  236.    }
  237. };
  238.